package jsu.lipengcheng.trafficnet.controlle.admin;

import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;
import jsu.lipengcheng.trafficnet.common.PageResult;
import jsu.lipengcheng.trafficnet.common.Result;
import jsu.lipengcheng.trafficnet.common.TicketDataListener;
import jsu.lipengcheng.trafficnet.pojo.Ticket;
import jsu.lipengcheng.trafficnet.pojo.TicketImportDTO;
import jsu.lipengcheng.trafficnet.pojo.TicketVO;
import jsu.lipengcheng.trafficnet.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/admin/ticket")
public class AdminticketControlle {
    @Autowired
    private TicketService ticketService;
    /**
     * 查询所有用户罚单
     * @return
     */
    @GetMapping
    public Result selectall(Integer userId, @RequestParam(required = false) LocalDate createTime,int page, int pageSize){
        PageResult pageResult = ticketService.selectall(userId,createTime,page,pageSize);
        return Result.success(pageResult);
    }
    @PostMapping("/add")
    public Result insert(@RequestBody Ticket ticket){
        ticketService.insertticket(ticket);
        return Result.success();
    }

    /**
     * 根据id查询用户罚单
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id){
        Ticket ticket= ticketService.selectbyid(id);
        return Result.success(ticket);
    }

    /**
     * 修改用户罚单
     * @param ticket
     * @return
     */
    @PutMapping("/update")
    public Result updateticket(@RequestBody Ticket ticket){
        ticketService.updateticket(ticket);
        return Result.success();
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") int id){
        ticketService.deleteticket(id);
        return Result.success();
    }

    @GetMapping("/sum")
    public Result sum(){
        int sum=  ticketService.selectsum();
        return Result.success(sum);
    }

    @GetMapping("/donwload")
    public void donwload(HttpServletResponse response) throws IOException {
        ticketService.donwload(response);
    }

    @PostMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "文件为空，请上传有效的 Excel 文件";
        }

        try {
            TicketDataListener listener = new TicketDataListener(ticketService);
            EasyExcel.read(file.getInputStream(), TicketImportDTO.class, listener)
                    .sheet()
                    .doRead();
            return "导入成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "导入失败：" + e.getMessage();
        } catch (Exception e) {
            e.printStackTrace();
            return "解析失败，请检查文件格式是否正确";
        }
    }

}
